package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.module.base.repository.mapper.DictMapper;
import com.gnerv.battle.boot.module.base.model.bo.DictBO;
import com.gnerv.battle.boot.module.base.model.dto.DictDTO;
import com.gnerv.battle.boot.module.base.model.dto.DictDetailsDTO;
import com.gnerv.battle.boot.module.base.model.entity.Dict;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.DictService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Service
@AllArgsConstructor
public class DictServiceImpl implements DictService {

    private final DictMapper dictMapper;
    private final CurrentAccountService currentAccountService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean create(@NonNull DictBO dictBO) {
        Dict dict = dictBO.toCreateDict();
        dict.setCreator(currentAccountService.accountUkId());
        dictMapper.insert(dict);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteByUkId(@NonNull String ukId) {
        dictMapper.deleteByUkId(ukId);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateByUkId(@NonNull DictBO dictBO) {
        Dict dict = dictBO.toUpdateDict();
        dictMapper.updateByUkId(dict);
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public DictDTO selectByUkId(@NonNull String ukId) {
        return dictMapper.selectByUkId(ukId);
    }

    @Override
    public DictDetailsDTO selectDetailsByCode(@NonNull String code) {
        return dictMapper.selectDetailsByCode(code);
    }

    @Override
    @Transactional(readOnly = true)
    public List<DictDTO> listByName(String name) {
        return dictMapper.listByName(name);
    }

    @Override
    public List<DictDetailsDTO> listDetailsByName(String name) {
        return dictMapper.listDetailsByName(name);
    }

    @Override
    @Transactional(readOnly = true)
    public boolean isExist(@NonNull String code, String ukId) {
        return dictMapper.isExist(code, ukId) > 0;
    }
}




